home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindlink.net!news
- From: genew@mindlink.bc.ca (Gene Wirchenko)
- Newsgroups: comp.lang.c
- Subject: Re: More Modulus questions
- Date: Thu, 22 Feb 1996 06:59:51 GMT
- Organization: MIND LINK! - British Columbia, Canada
- Message-ID: <4gh4eh$gsj@fountain.mindlink.net>
- References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
- NNTP-Posting-Host: line172.nwm.mindlink.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- wzjn@ix.netcom.com (KPN ) wrote:
-
- [snip]
-
- >Here again is my problem: (From æHow to Program CÆ second edition
- >Deitel/Dietel - Write a program that reads an integer and determines,
- >and prints how many digits in the integer are 7Æs)
- >
- >#include <stdio.h>
-
- >main()
-
- int main(void)
- is clearer.
-
- >{
- > int number;
- > int first, second, third, fourth;
- > int integer1, integer2, integer3, integer4;
-
- > printf("Enter a four digit number: ");
- > scanf("%d", &number);
-
- >/*
- > Split the four digit number into four seperate integers
- >*/
-
- > first = number / 1000;
- > integer1 = number % 1000;
-
- > second = integer1 / 100;
- > integer2 = integer1 % 100;
-
- > third = integer2 / 10;
- > integer3 = integer2 % 10;
-
- > fourth = integer3 / 1;
- > integer4 = integer3 % 1;
-
- Replace previous two lines of code with:
- fourth=integer3;
- Dividing by 1 is boring.
-
- >/*
- > Use modulas to determine if any integers are a 7
- >*/
-
- This doesn't work. 7%7 is the remainder from 7 divided by 7: 0.
- 0 is false and the if goes to the else. The logic is precisely
- reverse if the digit is not 7.
-
- >
- > if (first % 7)
-
- Try:
- if (first==7)
- and same with the others.
-
- > printf("First integer was a 7\n");
- > else
- > printf("Not a 7\n");
-
- >if (second % 7)
- > printf("second integer was a 7\n");
- > else
- > printf("Not a 7\n");
-
- >if (third % 7)
- > printf("third integer was a 7\n");
- > else
- > printf("Not a 7\n");
-
- >if (fourth % 7)
- > printf("fourth integer was a 7\n");
- > else
- > printf("Not a 7\n");
-
-
- >Yes, I know that you may have a different style/ would write it
- >differently / wouldnÆt do it this way, but thatÆs OK - IÆll learn. All
- >that IÆm concerned about right now, is that if I enter the number 7000,
- >it tells me that the first digit is not a seven, and all the rest are a
- >7.
-
- Please note that program was supposed to COUNT the number of 7s.
- So you really ought to do something like:
-
- int howmany;
- ...
- howmany=0
- if (first==7)
- howmany++;
- if (second==7)
- howmany++;
- and so on.
- printf("%d has %d sevens.\n", number,howmany);
-
- >Could this be explained to me in verbiage instead of a big complicated
- >example using all sorts of code that I do not know yet? I know, that in
- >the book IÆm teaching myself from, more than a few examples have IF
- >statement conditioned by a Modulus code ... how (in words!!!) does this
- >code work?
-
- I have not written the code the way I would do it, but have just
- corrected your worst. Have another look at it because...
- Your approach is deficient in that it will not handle bigger
- numbers. If you have not yet covered looping, forget this until then
- and then come back to it. If you have, you have the tools to deal
- with it. If you need further assistance, post code here or in E-mail.
-
- >Thank you for your patience and expertise,
-
- >kevin
-
- Sincerely,
-
- Gene Wirchenko
-
- C Pronunciation Guide:
- y=x++; "wye equals ex plus plus semicolon"
- x=x++; "ex equals ex doublecross semicolon"
-
-